package me.hl.concurrent;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * Created by Planck on 2016/1/29.
 */
public class ScheduledExecutorServiceTest {
    public static void main(String args[]) {


        final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        final Runnable runnable = new Runnable() {
            int count = 1;

            @Override
            public void run() {
                System.out.println("is " + count++ + " second");
            }

        };

        //1秒运行一次 0秒后运行 启动runnable这个线程
        final ScheduledFuture scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
        //三十分钟后关闭
        scheduledExecutorService.schedule(() -> {
            scheduledFuture.cancel(true);
            scheduledExecutorService.shutdown();
        }, 10, TimeUnit.SECONDS);

    }

}
